package com.anhry.app.safety.manage.enterprise.struts.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.enterprise.bean.TbCorpType;
import com.anhry.app.safety.manage.enterprise.struts.base.EnterpriseBaseAction;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Util;

/**
 * @date:Jul 11, 2012
 * @remark:
 * @author:dongbin
 */

@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class UtilDataAction extends EnterpriseBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 加载属地
	 */
	public void getDept(){
		this.contextPvd.getResponse().setCharacterEncoding("utf-8");
		List<TbUserDept> list = null;
		StringBuffer opts = new StringBuffer();
		try {
			list = this.getDeptList(this.cUnit,this.cCounty);
			if(list != null && list.size() > 0){
				opts.append("{");  //保存选项信息
				int counter = 0;                            //计数器
				for(TbUserDept dept:list){
//					如果不是第一项，追加一个“,”用于分隔选项	{'id':'name','id1':'name1'}
	                if (counter > 0) {
	                    opts.append(",");
	                }
	                opts.append("'");
	                opts.append(dept.getId());
	                opts.append("':'");
	                opts.append(dept.getDeptName());
	                opts.append("'");
	                counter++;                      //计数器加1	
				}
				opts.append("}");
			}
			this.contextPvd.getResponse().getWriter().write(opts+"");
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			Loggers.error("manager:UtilDataAction:getDept()出错："+e);
			e.printStackTrace();
		}
	}
	
	/**
	 * 加载企业类型
	 */
	public void getCorpType(){
		this.contextPvd.getResponse().setCharacterEncoding("utf-8");
		List<TbCorpType> list = null;
		StringBuffer opts = new StringBuffer();
		try {
			list = this.getTypeList(this.cMaxType);
			if(list != null && list.size() > 0){
				opts.append("{");  //保存选项信息
				int counter = 0;                            //计数器
				for(TbCorpType type:list){
//					如果不是第一项，追加一个“,”用于分隔选项	{'id':'name','id1':'name1'}
					if (counter > 0) {
						opts.append(",");
					}
					opts.append("'");
					opts.append(type.getId());
					opts.append("':'");
					opts.append(type.getTypeName());
					opts.append("'");
					counter++;                      //计数器加1	
				}
				opts.append("}");
			}
			this.contextPvd.getResponse().getWriter().write(opts+"");
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			Loggers.error("manager:UtilDataAction:getCorpType()出错："+e);
			e.printStackTrace();
		}
	}
	
	public List getDeptList(String unitId,String method){
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();
		List<TbUserDept> deptList = (List)this.contextPvd.getServletContextAttr(Util.SD_DEPT_LIST);
		List list = new ArrayList();
		if(null!=deptList && deptList.size()>0){
			for(TbUserDept dept : deptList){
				if("7".equals(grade) && null!=method){//属地用户
					if(dept.getId().toString().equals(user.getTbUserDept().getId().toString())){
						list.add(dept);
						break;
					}
				}else{
					if(dept.getDeptParentId().toString().equals(unitId)){
						list.add(dept);
					}
				}
			}
		}
		return list;
	}
	public List getTypeList(String typeId){
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();
		List<TbCorpType> typeList = (List)this.contextPvd.getServletContextAttr(Util.ALL_CORP_TYPE);
		List list = new ArrayList();
		try {
		/*	if("4".equals(grade) && "3".equals(user.getTbUserDept().getDeptClass())){//区科室行业部门
				//list = (List)this.utilService.getHqlQuery("from TbCorpType t where 1=1 and t.typeQudeptIds like '%"+user.getTbUserDept().getId()+"%' and t.isDele=0 order by t.id");
			}else{*/
				if(null!=typeList && typeList.size()>0){
					for(TbCorpType type : typeList){
						if("3".equals(grade) && "3".equals(user.getTbUserDept().getDeptClass())){//市科室的行业监管部门
							if(type.getTypeParentId().toString().equals(typeId) && type.getUnitId().toString().equals(user.getTbUserDept().getId().toString())){
								list.add(type);
							}
						}else if("5".equals(grade) && "1".equals(user.getTbUserUnit().getDeptClass())){//市行业，只取得其监管的企业类型
							if(type.getTypeParentId().toString().equals(typeId) && type.getUnitId().toString().equals(user.getTbUserUnit().getId().toString())){
								list.add(type);
							}
						}else if("6".equals(grade) && "1".equals(user.getTbUserDept().getDeptClass())){//区行业，取得上级市行业相同企业类型
							if(type.getTypeParentId().toString().equals(typeId) && type.getUnitId().toString().equals(user.getTbUserDept().getDeptSj().toString())){
								list.add(type);
							}
						}else if("4".equals(grade) && "3".equals(user.getTbUserDept().getDeptClass())){//区科室行业部门
							//list = (List)this.utilService.getHqlQuery("from TbCorpType t where 1=1 and t.typeQudeptIds like '%"+user.getTbUserDept().getId()+"%' and t.isDele=0 order by t.id");
							if(type.getTypeQudeptIds() != null) {
								if(type.getTypeParentId().toString().equals(typeId) && type.getTypeQudeptIds().contains(user.getTbUserDept().getId().toString())){
									list.add(type);
								}
							}
						}else{
							if(type.getTypeParentId() != null && type.getTypeParentId().toString().equals(typeId)){
								list.add(type);
							}
						}
					}
				//}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
